package com.mazegame.frontend;

import com.mazegame.backend.BackgroundPanel;
import com.mazegame.backend.ImageDisplayPane;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import javax.swing.*;
import java.awt.*;

public class OptionPanel extends JPanel implements GamePanel{
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;

    private ModeCard selectedMode = null;
    private ModeCard goldMode = null;
    private ModeCard storyMode = null;

    public OptionPanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;//
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);
        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/游戏模式选择界面.png");
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);
        // 创建 JavaFX 内容面板（登录界面）
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层
        Platform.runLater(() -> {
            // 创建主容器
            BorderPane root = createOptionPane();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }

    private BorderPane createOptionPane(){
        BorderPane root = new BorderPane();
        root.setTop(createTitleRegion());
        root.setCenter(createOptionRegion());
        root.setBottom(createControlRegion());
        return root;
    }

    private Region createTitleRegion() {
        HBox titleBox = new HBox();
        titleBox.setPadding(new Insets(20, 0, 0, 30));
        // 设置宽高
        titleBox.setPrefSize(WIDTH, 120);
        // 设置背景颜色,辅助布局
        //titleBox.setStyle("-fx-background-color: rgb(255,255,255);");
        javafx.scene.control.Button button = new Button();
        ImageDisplayPane imageDisplayPane = new ImageDisplayPane("/img/返回按钮.png", 80, 80, 0);
        button.setGraphic(imageDisplayPane);
        button.setStyle("-fx-background-color:transparent; -fx-background-radius: 200;");
        button.setEffect(null);
        button.setPrefSize(100, 100);
        // 设置最大宽高度
        button.setMaxSize(100, 100);
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            button.setStyle("-fx-background-color: rgba(255,255,255,0.5); -fx-background-radius: 200;");
        });
        button.setOnMouseExited(e -> {
            button.setStyle("-fx-background-color: transparent; -fx-background-radius: 200;");
            button.setEffect(null);
        });
        // 添加点击效果
        button.setOnAction(e -> {
            cardLayout.show(cardPanel, "MAINPANEL");
        });
        titleBox.getChildren().add(button);
        return titleBox;
    }

    private Region createOptionRegion() {
        HBox modeBox = new HBox(120); // 两个模式卡片间距
        modeBox.setPadding(new Insets(20));
        modeBox.setAlignment(Pos.TOP_CENTER);
        modeBox.setPrefHeight(600);
        // 剧情模式卡片
        storyMode = new ModeCard(
                "剧情模式",
                "/img/剧情.png",
                "体验完整主线剧情，探索星象神庙的秘密。",
                new String[] {"主线剧情解锁", "角色技能", "迷宫道具"}
        );
        // 金币模式卡片
        goldMode = new ModeCard(
                "金币模式",
                "/img/剧情.png",
                "限时挑战副本，获取高额金币奖励。",
                new String[] {"金币掉落", "限时任务", "排行榜竞争"}
        );
        // 添加选择事件处理
        storyMode.setOnMouseClicked(e -> selectMode(storyMode));
        goldMode.setOnMouseClicked(e -> selectMode(goldMode));
        // 默认选中剧情模式
        //selectMode(storyMode);
        modeBox.getChildren().addAll(storyMode, goldMode);
        return modeBox;
    }

    private Region createControlRegion() {
        HBox controlBox = new HBox(180);
        controlBox.setPadding(new Insets(20));
        controlBox.setAlignment(Pos.TOP_CENTER);
        controlBox.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
        controlBox.setPrefHeight(180);
        Button startButton = createButton("开始游戏");
        // 添加点击事件
        startButton.setOnAction(e -> {
            if (selectedMode == storyMode) {
                ChapterPanel.update();
                cardLayout.show(cardPanel, "CHAPTERPANEL");
            }else if (selectedMode == goldMode) {
                cardLayout.show(cardPanel, "MAZEGENERATEPANEL2");
            }
            //取消选中
            selectedMode.setSelected(false);
        });

        //controlBox.getChildren().addAll(backButton, startButton);
        controlBox.getChildren().add(startButton);
        return controlBox;
    }

    // 模式卡片类
    private class ModeCard extends VBox {
        private String name;
        private String imageUrl;
        private String description;
        private String[] features;
        private javafx.scene.control.Label selectedBadge;

        public ModeCard(String name, String imageUrl, String description, String[] features) {
            this.name = name;
            this.imageUrl = imageUrl;
            this.description = description;
            this.features = features;

            setPrefSize(500, 600);
            setSpacing(12);
            setPadding(new Insets(20));
            setStyle("-fx-background-color:  rgba(99,110,114,0.2); " +
                            "-fx-background-radius: 24; " +
                            "-fx-border-radius: 24; " +
                            "-fx-border-width: 3; " +
                            "-fx-border-color: rgba(108, 92, 231, 0.3); " +
                            "-fx-effect: none;");
            // 添加选中标记
            selectedBadge = new Label("已选择");
            selectedBadge.setTextFill(Color.WHITE);
            selectedBadge.setFont(javafx.scene.text.Font.font("Arail", FontWeight.BOLD, 24));
            selectedBadge.setStyle("-fx-background-color: rgba(255,234,0,1); -fx-background-radius: 20; -fx-padding: 10;");
            selectedBadge.setVisible(false);
            // 创建容器用于叠加标记和图片
            Pane imageContainer = new StackPane();
            imageContainer.setPrefSize(460, 280); // 设置容器大小
            // 添加模式图片
            ImageDisplayPane imagePane = new ImageDisplayPane(imageUrl, 460, 280, 45);
            imageContainer.getChildren().add(imagePane);
            // 将 selectedBadge 放置在容器的左上角
            imageContainer.getChildren().add(selectedBadge);
            StackPane.setAlignment(selectedBadge, Pos.TOP_LEFT); // 设置对齐方式为左上角
            // 添加模式名称
            Label nameLabel = new Label(name);
            nameLabel.setFont(javafx.scene.text.Font.font("KaiTi", FontWeight.BOLD, 32));
            nameLabel.setTextFill(Color.WHITE);
            // 添加描述
            Label descLabel = new Label(description);
            descLabel.setFont(javafx.scene.text.Font.font("KaiTi", 24));
            descLabel.setTextFill(Color.WHITE);
            descLabel.setWrapText(true);
            descLabel.setPrefWidth(460);
            // 添加特色信息
            VBox featuresBox = new VBox(5);
            featuresBox.setAlignment(Pos.CENTER_LEFT);
            Label featuresTitle = new Label("模式特色:");
            featuresTitle.setFont(javafx.scene.text.Font.font("KaiTi", FontWeight.BOLD, 24));
            featuresTitle.setTextFill(Color.WHITE);
            featuresBox.getChildren().add(featuresTitle);
            for (String feature : features) {
                javafx.scene.control.Label featureLabel = new Label("• " + feature);
                featureLabel.setFont(Font.font("KaiTi", 24));
                featureLabel.setTextFill(Color.WHITE);
                featuresBox.getChildren().add(featureLabel);
            }
            // 添加到卡片
            getChildren().addAll(imageContainer, nameLabel, descLabel, featuresBox);
            // 添加悬停效果
            setOnMouseEntered(e -> {
                if (!isSelected()) {
                    setStyle("-fx-background-color: rgba(99, 110, 114, 0.2); " +
                            "-fx-background-radius: 24;" +
                            " -fx-border-radius: 24; " +
                            "-fx-border-width: 3; " +
                            "-fx-border-color: rgba(237,223,18,0.8); " +
                            "-fx-effect: dropshadow(gaussian, rgba(255, 233, 0, 0.5), 15, 0, 0, 0);");
                }
            });
            setOnMouseExited(e -> {
                if (!isSelected()) {
                    setStyle("-fx-background-color: rgba(99, 110, 114, 0.2);" +
                            " -fx-background-radius: 24;" +
                            " -fx-border-radius: 24;" +
                            " -fx-border-width: 3;" +
                            " -fx-border-color: rgba(108, 92, 231, 0.3);" +
                            " -fx-effect: none;");
                }
            });
        }
        public boolean isSelected() {
            return selectedBadge.isVisible();
        }
        public void setSelected(boolean selected) {
            if (selected) {
                selectedBadge.setVisible(true);
                setStyle("-fx-background-color:  rgba(99,110,114,0.2); " +
                        "-fx-background-radius: 24; " +
                        "-fx-border-radius: 24; " +
                        "-fx-border-width: 3; " +
                        "-fx-border-color: rgba(237,223,18,0.8);  " +
                        "-fx-effect: dropshadow(gaussian, rgba(255, 233, 0, 0.5), 15, 0, 0, 0);");
            } else {
                selectedBadge.setVisible(false);
                setStyle("-fx-background-color:  rgba(99,110,114,0.2); " +
                        "-fx-background-radius: 24; " +
                        "-fx-border-radius: 24; " +
                        "-fx-border-width: 3; " +
                        "-fx-border-color: rgba(108, 92, 231, 0.3); " +
                        "-fx-effect: none;");

            }
        }
    }
    private void selectMode(ModeCard mode) {
        // 取消之前选中的模式
        if (selectedMode != null) {
            selectedMode.setSelected(false);
        }
        // 设置新选中的模式
        selectedMode = mode;
        selectedMode.setSelected(true);
    }
    private Button createButton(String text) {
        Button button = new Button(text);
        button.setFont(Font.font("KaiTi", FontWeight.BOLD, 40));
        button.setTextFill(Color.WHITE);
        button.setPrefSize(WIDTH/6, 65);
        button.setStyle("-fx-background-color: #6C5CE7; -fx-background-radius: 10;");
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            button.setStyle("-fx-background-color: #8C7DF7; -fx-background-radius: 10;");
            button.setEffect(new DropShadow(25, Color.rgb(108, 92, 231, 0.5)));
        });

        button.setOnMouseExited(e -> {
            button.setStyle("-fx-background-color: #6C5CE7; -fx-background-radius: 10;");
            button.setEffect(null);
        });

        return button;
    }
    @Override
    public void showPanel() {
        cardLayout.show(cardPanel, "OPTIONPANEL");
    }
    @Override
    public void cleanup() {}
}
